package com.majun.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author mj
 * @2025/2/10 2:26
 */
public class gitTest2 {
    public static void main(String[] args) {
        // 指定 Git 仓库目录
        String gitRepoPath = "F:/majungitdiff/majun2024";

        // 指定要比较的两个 commit ID
        String commitId1 = "0f314052dd8dc36648cdb1d9ae9b8e21fbf6975e";
        String commitId2 = "2a55ff6ec53c9c7f7b9846d29f6be7105a8dbfdc";

        // 构建 git diff 命令，使用 -C 选项指定仓库目录，并比较两个 commit
        List<String> command = new ArrayList<>();
        command.add("git");
        command.add("-C");
        command.add(gitRepoPath);
        command.add("diff");
        command.add("--name-status");
        command.add(commitId1);
        command.add(commitId2);

        try {
            // 执行命令并获取输出
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {
                // 解析输出
                String[] parts = line.trim().split("\\s+", 2); // 使用 limit 2 来确保不会拆分文件名中的空格
                if (parts.length == 2) {
                    String status = parts[0];
                    String fileName = parts[1];

                    // 打印结果
                    System.out.println("Status: " + status + ", File: " + fileName);
                }
            }

            // 等待进程完成并检查退出码
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                System.err.println("Git diff command failed with exit code: " + exitCode);
            }

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
